package cn.t.repository.impl.device;

import cn.t.converter.device.AngleConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.device.AngleDO;
import cn.t.persistence.entity.device.AnglePO;
import cn.t.persistence.mapper.device.AngleMapper;
import cn.t.repository.device.AngleRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 旋转柜角度配置表(Angle)数据仓库
 *
 * @author t
 * @since 2024-12-09 10:38:00
 */
@Slf4j
@Repository
@AllArgsConstructor
public class AngleRepositoryImpl extends RepositoryImpl<AngleMapper, AnglePO, AngleConverter, AngleDO> implements AngleRepository {


    @Override
    public List<AngleDO> findByModel(AngleDO angleDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;

        List<AnglePO> list = getWrapper(angleDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<AnglePO> getWrapper(AngleDO angleDO) {
        LambdaQueryChainWrapper<AnglePO> wrapper = lambdaQuery();
        if (ObjectUtils.isEmpty(angleDO.getLayerCargowayNum())) {
            wrapper.eq(AnglePO::getLayerCargowayNum, angleDO.getLayerCargowayNum());
        }
        if (ObjectUtils.isEmpty(angleDO.getCargowayNo())) {
            wrapper.eq(AnglePO::getCargowayNo, angleDO.getCargowayNo());
        }
        if (ObjectUtils.isEmpty(angleDO.getAngle())) {
            wrapper.eq(AnglePO::getAngle, angleDO.getAngle());
        }
        if (ObjectUtils.isEmpty(angleDO.getGmtModified())) {
            wrapper.between(AnglePO::getGmtModified, angleDO.getGmtModifiedStart(), angleDO.getGmtModifiedEnd());
        }
        if (ObjectUtils.isEmpty(angleDO.getGmtCreated())) {
            wrapper.between(AnglePO::getGmtCreated, angleDO.getGmtCreatedStart(), angleDO.getGmtCreatedEnd());
        }
        return wrapper;
    }

    @Override
    public long count(AngleDO angleDO) {
        return getWrapper(angleDO).count();
    }

    @Override
    public List<AngleDO> getAll() {
        LambdaQueryChainWrapper<AnglePO> wrapper = lambdaQuery();
        List<AnglePO> list = wrapper.list();
        return converter.pd2DoList(list);
    }
}
